var recordStartX = 0
var currentOffsetX = 0
import mode from '../../utils/mode'
const app = getApp()
Page({
    data: {
        id: '',
        detailList: null,
        cIndex: 0,
        addressId: '',
        addressType: 0, //0 正常显示  1.其他页面过来有数据  2其他页面过来无数据
        orderId: '',
        selItem: null,
        is_integral: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var uinfo = wx.getStorageSync('uinfo')
        this.setData({
            id: uinfo.user.id
        })
        if (options.addressId) {
            this.setData({
                addressId: options.addressId,
            })
        }
        if (options.addressType) {
            this.setData({
                addressType: options.addressType,
                orderId: options.orderId || ''
            })
            options.orderId && options.orderType && this.getOrdersInfo(options.orderType, options.orderId)
        }
        if (options.integral) {
            this.setData({ is_integral: options.integral })
        }
    },
    async getOrdersInfo (orderType, orderId) {
        let { code, data } = await mode.getOrdersInfo({
            orders_type: orderType,
            user_id: this.data.id,
            id: orderId
        })
        if (code) {
            this.setData({
                addressId: data.address.address_id || '',
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getAddress()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    bindPickerChangeBirth (...e) {
        console.log(`%c✅eE=> `, 'color:#ff7b54;font-size:16px;', e)
    },
    checkedChange (e) {
        let { index } = e.currentTarget.dataset
        this.setData({
            addressId: index.id,
            selItem: index
        })
    },
    getAddress () { //获取列表
        var that = this
        if (!that.data.id) return
        wx.$api.getAddress({
            id: that.data.id
        }).then(res=>{
            if(res.code){
                let { addressId } = this.data
                that.setData({
                    detailList: res.data || [],
                    selItem: addressId ? res.data.filter(x => x.id == addressId)[0] : null
                })
            }
        })
    },
    handleClick (e) { //修改
        let { item } = e.currentTarget.dataset
        wx.navigateTo({
            url: '/pages/address-save/address-save?type=2&address_id=' + item.id
        })
    },

    handleClickDel (e) {
        var that = this
        console.log(e.currentTarget.dataset.index.id)
        wx.showModal({
            title: '提示',
            content: '是否确认删除该地址',
            success: function (res) {
                if (res.confirm) {
                    wx.$api.delAddress( {
                            id: e.currentTarget.dataset.index.id
                        }).then(res=>{
                            if(res.code){
                                wx.showToast({
                                    title: '删除成功',
                                    icon: 'success',
                                    duration: 1000
                                })
                                setTimeout(() => {
                                    that.getAddress()
                                }, 1500)
                            }else{
                                wx.showToast({
                                    title: '删除失败',
                                    icon: "error",
                                    duration: 1000
                                })
                            }
                            
                    })
                }
            }
        })


    },

    handleClickSave () {
        let { addressId, orderId, selItem } = this.data
        if (!addressId) {
            wx.showToast({
                title: '请选择一个地址！',
                icon: 'none'
            })
        } else {
            if (orderId) {
                wx.showModal({
                    title: '提示',
                    content: '确定要修改订单地址吗？',
                    success: async (res) => {
                        if (res.confirm) {
                            wx.showLoading()
                            let { id, name, phone, address, addressdetail } = selItem
                            let reqData = {
                                id: orderId,
                                address_id: id,
                                name,
                                phone,
                                address,
                                addressdetail,
                                type: '2',
                            }
                            this.data.is_integral == 1 ? reqData.is_integral = 1 : ''
                            let { code, data } = await mode.saveOrderAddress(reqData)
                            wx.hideLoading()
                            if (code == 1) {
                                wx.showToast({ title: '地址修改成功!', icon: 'none' })
                                setTimeout(() => {
                                    app.backPage()
                                }, 300)
                            } else {
                                wx.showToast({
                                    title: '修改地址失败！',
                                    icon: 'error'
                                })
                            }
                        }
                    }
                })
            } else {

                let pages = getCurrentPages()
                let prevPage = pages[pages.length - 2]
                prevPage.setData({
                    addressId: this.data.addressId
                })
                app.backPage()
            }
        }
    },

    recordStart: function (e) {
        this.setData({
            cIndex: e.currentTarget.dataset.index
        })
        recordStartX = e.touches[0].clientX
        currentOffsetX = this.data.detailList[e.currentTarget.dataset.index].offsetX
        // console.log('start x ', recordStartX);
    },
    recordMove: function (e) {
        var detailList = this.data.detailList
        var item = detailList[this.data.cIndex]
        var x = e.touches[0].clientX
        var mx = recordStartX - x
        // console.log('move x ', mx);

        var result = currentOffsetX - mx
        if (result >= -80 && result <= 0) {
            item.offsetX = result
        }
        this.setData({
            detailList: detailList
        })
    },
    recordEnd: function (e) {
        var detailList = this.data.detailList
        var item = detailList[this.data.cIndex]

        if (item.offsetX < -40) {
            item.offsetX = -80

        } else {
            item.offsetX = 0

        }
        this.setData({
            detailList: detailList
        })
    },
    onClose (event) {
        const { position, instance } = event.detail
        switch (position) {
            case 'left':
            case 'cell':
                instance.close()
                break
            case 'right':
                Dialog.confirm({
                    message: '确定删除吗？',
                }).then(() => {
                    instance.close()
                })
                break
        }
    },

})